package com.lingshi.yingzaitownmap.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lingshi.yingzaitownmap.entity.domain.Event;
import com.lingshi.yingzaitownmap.entity.dto.PageDTO;
import com.lingshi.yingzaitownmap.entity.result.PageBean;
import com.lingshi.yingzaitownmap.mapper.EventMapper;
import com.lingshi.yingzaitownmap.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EventServiceImpl implements EventService {

    @Autowired
    private EventMapper eventMapper;

    @Override
    public PageBean<Event> select(PageDTO eventPageDTO) {

        // 设置默认页码和每页数量
        if(eventPageDTO.getPage() == null || eventPageDTO.getPage() < 1){
            eventPageDTO.setPage(1);
        }
        if (eventPageDTO.getPageSize() == null || eventPageDTO.getPageSize() < 1){
            eventPageDTO.setPageSize(10);
        }

        // 设置分页
        PageHelper.startPage(eventPageDTO.getPage(), eventPageDTO.getPageSize());
        // 查询
        List<Event> list = eventMapper.select(eventPageDTO.getKeyword());
        // 获取分页结果
        Page<Event> p = (Page<Event>) list;
        // 返回分页结果
        return new PageBean<>(p.getResult(), p.getTotal());
    }

    @Override
    public Event getById(String id) {
        // 查询并返回结果
        return eventMapper.getById(id);
    }
}
